let app = getApp();
import Toast from '../../../miniprogram/miniprogram_npm/vant-weapp/toast/toast'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderInfo:{},
    shop:'',
    user: '',
    value:5,//星级
    fileList: [],//图片列表
    content:'',//文本
    imgList:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let orderInfo = wx.getStorageSync('orderInfo');
    this.setData({
      orderInfo:orderInfo
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let _this = this;
    _this.setData({
      shop: app.getShop(),
      user: app.getUser(),
    })
  },
  onChange(event) {
    this.setData({
      value: event.detail,
    });
  },
  beforeRead(event) {
    const { file, callback } = event.detail;
    callback(file.type === 'image');
  },
  // 上传
  afterRead(event) {
    let that = this;
    const { file } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: app.globalData.deployUrl + '/platform/config/upload', // 仅为示例，非真实的接口地址
      filePath: file.path,
      name: 'file',
      formData: { user: 'test' },
      success(res) {
        // 上传完成需要更新 fileList
        let data = JSON.parse(res.data);
        let fileList = that.data.fileList;
        fileList.push({ ...file, url: data.key });
        that.data.imgList.push(data.key);
        that.setData({ fileList });
      },
    });
  },
  delete(event){
    let index = event.detail.index;
    let that = this;
    that.data.fileList.splice(index,1);
    that.setData({
      fileList:that.data.fileList
    })
  },
  contentFunction:function(event){
    let that = this;
    that.setData({
      content:event.detail.value
    })
  },
  getComment:function(){
    let that = this;
    if(that.data.content == '' || !that.data.content){
      Toast('请填写文本');
      return;
    }else if(that.data.fileList == '' || that.data.fileList.length == 0){
      Toast('请最少上传一张图片');
    }else{
      app.requestUrl({
        method: 'POST',
        url: '/wxapi/product/comment/add',
        data: {
          mid:that.data.orderInfo.mid,
          orderid:that.data.orderInfo.id,
          content:that.data.content,
          imageList:that.data.imgList,
          level:that.data.value,
        }
      }).then((res)=>{
        if(res.data.code == 0){
          Toast(res.data.msg);
          setTimeout(function(){
            wx.navigateBack({
              delta: -1
            });
          },500)
        }
        else
          Toast(res.data.msg);
      })
    }
  },
})